package com.gitee.l0km.beanfilter.core.context;

import java.util.Objects;

import com.gitee.l0km.beanfilter.core.CodecScene;
import com.gitee.l0km.beanfilter.core.IFilter;
import com.gitee.l0km.beanfilter.core.RequestPhase;

/**
 * 过滤器工作上下文，提供过滤器当前工作环境基本信息
 * @author guyadong
 *
 */
public class FilterContext {
	private final IFilter filter;
	private final Class<?> beanClass;
	public FilterContext(IFilter filter, Class<?> beanClass) {
		this.filter = filter;
		this.beanClass = beanClass;
	}
	/**
	 * 当前过滤器
	 */
	public IFilter getFilter() {
		return filter;
	}
	/**
	 * 当前过滤的Java Bean类型
	 */
	public Class<?> getBeanClass() {
		return beanClass;
	}

	/**
	 * 当前序列化的对象，反序列化时返回{@code null}
	 */
	public Object getBean() {
		return LocalContext.getCurrentContext().getBean();
	}
	/**
	 * 当前过滤器工作的请求阶段
	 */
	public RequestPhase getRequestPhase() {
		return LocalContext.getCurrentContext().getRequestPhase();
	}
	/**
	 * 当前过滤器工作的场景
	 */
	public CodecScene getScene() {
		return LocalContext.getCurrentContext().getScene();
	}
	@Override
	public int hashCode() {
		return Objects.hash(beanClass, filter);
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FilterContext other = (FilterContext) obj;
		return Objects.equals(beanClass, other.beanClass) && Objects.equals(filter, other.filter);
	}
}
